package com.gary.baiduai.facematch;

import com.gary.baiduai.common.BaiduAIAuthor;
import com.gary.baiduai.utils.FileUtil;
import com.gary.baiduai.utils.GsonUtils;
import com.gary.baiduai.utils.HttpUtil;

import java.util.*;

/**
* 人脸对比
*/
public class FaceMatch {

    /**
    * 重要提示代码中所需工具类
    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
    * 下载
     *
     * 人脸比对 https://aip.baidubce.com/rest/2.0/face/v3/match
     *
    */
    public static String faceMatch() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";

        try {
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map1 = new TreeMap<>();
            String img1 = FileUtil.getImageStr("E:\\personal\\20210120100148.png");
            map1.put("image", img1);
            map1.put("image_type","BASE64");
            map1.put("face_type","LIVE");
            map1.put("quality_control","LOW");
            map1.put("liveness_control","NONE");
            list.add(map1);

            Map<String, Object> map2 = new HashMap<>();
            String img2 = FileUtil.getImageStr("E:\\personal\\20210118165957.jpg");
            map2.put("image", img2);
            map2.put("image_type","BASE64");
            map2.put("face_type","LIVE");
            map2.put("quality_control","LOW");
            map2.put("liveness_control","NONE");
            list.add(map2);

            String param = GsonUtils.toJson(list);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "25.ff9cb0594ed643f107dfe916ddde5f2f.315360000.1926468230.282335-22843808";

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 皮肤分析 https://aip.baidubce.com/rest/2.0/face/v1/skin_analyze
     * @return
     */
    public static String skinAnalyze() {

        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v1/skin_analyze";

        try {
            Map<String, Object> map1 = new HashMap<>();
            String img1 = FileUtil.getImageStr("E:\\personal\\20210119093304.jpg");
            map1.put("image", img1);
            map1.put("image_type","BASE64");
            //包括 color,smooth,acnespotmole,wrinkle,eyesattr,blackheadpore 信息，对应6项检测能力，用逗号分隔，次序可调换。 默认只返回face_token、location信息
            map1.put("face_field","color,smooth,acnespotmole,wrinkle,eyesattr,blackheadpore");
            map1.put("max_face_num",10);

            String param = GsonUtils.toJson(map1);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = BaiduAIAuthor.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String s = FaceMatch.skinAnalyze();
        System.out.println(s);
    }
}